import ChartWithAxis from './chartWithAxis';

class BarChart extends ChartWithAxis {
    getOption(params) {
        super.getOption(params);
        this.addAxises(params);
        this.addSeries(params);
        // console.log('barChartOption:',params.title,this.option);
        // console.log(JSON.stringify(this.option))
        return this.option;
    }
    processBarSerieData(params){
        let data = params.data.measure;
        let newData = [];
        for(let i=0; i<data.length; i++){
            if(!data[i]){
                newData.push({
                    value:data[i],
                    label:{
                        normal:{
                            show:false
                        }
                    }
                })
            }else{
                newData.push({
                    value:data[i],
                    label:{
                        normal:{
                            show:true
                        }
                    }
                })
            }
        }
        return newData;
    }
    addSeries(params){
        let markLineAvg = params.data.preAvg;
        let lastDate = params.data.preDate;
        if(lastDate && lastDate.indexOf('-')>0){ //'2020-02-16'变成'2月16日'
            let idx = lastDate.indexOf('-');
            let monthDay = lastDate.substr(idx+1); //'02-16'
            let arr = monthDay.split('-'); //'['02','16']
            let month = arr[0][0]==0 ? arr[0][1] : arr[0];
            lastDate = month + '月' + arr[1] + '日';
        }
        let markLineAvgUnit = params.markLineAvgUnit || '';
        let series=[
            {
                name: '',
                type: 'bar',
                barWidth:params.barWidth || 7,
                itemStyle:{
                    normal:{
                        barBorderRadius: 20,
                        label:{
                            show:true,
                            position:'top',
                            fontSize:14,
                            formatter:function(value){
                                // console.log(value)
                                let idx = null;
                                if(value.value){
                                    idx = String(value.value).indexOf('.');
                                    if(params.yAxisLabelFormatter && params.yAxisLabelFormatter=='%'){
                                        if(idx > 0){ //有小数点
                                            //点后有两位
                                            if(String(value.value).substr(idx+1).length==2){
                                                return value.value * 100 +'%';
                                            }
                                            return (value.value * 100).toFixed(2) +'%';
                                        }else{
                                            return value.value * 100 +'%';
                                        }  
                                    }else{
                                        if(idx > 0){
                                            return (value.value).toFixed(2);
                                        }else{
                                            return value.value;
                                        }
                                    }
                                }
                            }
                        }
                    }
                },
                data:this.processBarSerieData(params)
            }
        ];
        // if(markLineAvg != null && (markLineAvg || markLineAvg >= 0 )){
        if(params.showMarkLine){
            this.showMarkLine(markLineAvg,lastDate,markLineAvgUnit,series,params);
        }
        this.option.series = series;
    }
    showMarkLine(markLineAvg,lastDate,markLineAvgUnit,series,params){
        //平均值保留两位小数
        let temp = String(markLineAvg).indexOf('.')>0 ? markLineAvg.toFixed(2) : markLineAvg;
        markLineAvg = Number(temp);

        //平均值超过柱子最大值则取最大值，线的标签显示真实均值，否则不出平均线
        let realAvg = markLineAvg;
        let measureData = params.data.measure;
        let max = Math.max.apply(null,measureData);
        realAvg = markLineAvg > max ? max : markLineAvg;
        
        series[0].markLine = {
            data: [
                [
                    {
                        name:lastDate,
                        value:realAvg,
                        coord:[0,realAvg],
                        symbol:'none'
                    },
                        {
                        name:lastDate,
                        value:realAvg,
                        coord:[4,realAvg],
                        symbol:'none'
                    }   
                ]
            ],
            label:{
                normal:{
                    // formatter:'{{b}\n整体人均1个}'
                    backgroundColor:'rgba(255,255,255,0.5)',
                    // borderWidth:1,
                    // borderColor:'red',
                    // borderRadius:8,
                    // padding:[1,8],
                    lineHeight:24,
                    color:'red',
                    align:'center',
                    fontSize: 14,
                    formatter:function(value){
                        return lastDate + ' 整体人均'+ markLineAvg + markLineAvgUnit;
                    },
                    position:'middle'
                }
            },
            lineStyle:{
                normal:{
                    color:'red',
                    width: 2
                }
            }
        }
    }
}

export default BarChart;